unit uParentBasic;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, cxGraphics, cxControls, cxLookAndFeels,
  cxLookAndFeelPainters, cxContainer, cxEdit, dxSkinsCore, dxSkinBlack,
  dxSkinBlue, dxSkinBlueprint, dxSkinCaramel, dxSkinCoffee, dxSkinDarkRoom,
  dxSkinDarkSide, dxSkinDevExpressDarkStyle, dxSkinDevExpressStyle, dxSkinFoggy,
  dxSkinGlassOceans, dxSkinHighContrast, dxSkiniMaginary, dxSkinLilian,
  dxSkinLiquidSky, dxSkinLondonLiquidSky, dxSkinMcSkin, dxSkinMoneyTwins,
  dxSkinOffice2007Black, dxSkinOffice2007Blue, dxSkinOffice2007Green,
  dxSkinOffice2007Pink, dxSkinOffice2007Silver, dxSkinOffice2010Black,
  dxSkinOffice2010Blue, dxSkinOffice2010Silver, dxSkinPumpkin, dxSkinSeven,
  dxSkinSevenClassic, dxSkinSharp, dxSkinSharpPlus, dxSkinSilver,
  dxSkinSpringTime, dxSkinStardust, dxSkinSummer2008, dxSkinTheAsphaltWorld,
  dxSkinsDefaultPainters, dxSkinValentine, dxSkinVS2010, dxSkinWhiteprint,
  dxSkinXmas2008Blue, Vcl.Menus, Vcl.StdCtrls, cxButtons, cxGroupBox,
  Vcl.ActnList, cxStyles, dxSkinscxPCPainter, cxCustomData, cxFilter, cxData,
  cxDataStorage, cxNavigator, Data.DB, cxDBData, cxGridLevel, cxClasses,
  cxGridCustomView, cxGridCustomTableView, cxGridTableView, cxGridDBTableView,
  cxGrid, ClientRowSet, BaseForm, dxSkinBasic, dxSkinMetropolis,
  dxSkinMetropolisDark, dxSkinOffice2013DarkGray, dxSkinOffice2013LightGray,
  dxSkinOffice2013White, dxSkinOffice2016Colorful, dxSkinOffice2016Dark,
  dxSkinOffice2019Black, dxSkinOffice2019Colorful, dxSkinOffice2019DarkGray,
  dxSkinOffice2019White, dxSkinTheBezier, dxSkinVisualStudio2013Blue,
  dxSkinVisualStudio2013Dark, dxSkinVisualStudio2013Light, dxSkinWXI,
  dxDateRanges, dxScrollbarAnnotations, System.Actions;

type
  TFrmParentBasic = class(TBaseForm)
    ActionList: TActionList;
    actNew: TAction;
    actEdit: TAction;
    actSave: TAction;
    actCancel: TAction;
    actDelete: TAction;
    actRefresh: TAction;
    cxGroupBox6: TcxGroupBox;
    cxButton15: TcxButton;
    cxButton14: TcxButton;
    cxButton13: TcxButton;
    cxButton12: TcxButton;
    cxButton11: TcxButton;
    cxButton1: TcxButton;
    gbSN: TcxGroupBox;
    GrdBasic: TcxGrid;
    tvBasic: TcxGridDBTableView;
    lvEXP_COMP: TcxGridLevel;
    procedure actNewExecute(Sender: TObject);
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
    FKeyFieldName: string;
    FDataSet: TClientRowSet;
    FDataSource: TDataSource;
    FCustomDeleteEvent, FCustomSaveEvent: Boolean;
    procedure DataChangeExt(Sender: TObject; Field: TField);
    procedure StateChangeExt(Sender: TObject);
  published
    function BeforePostCheck: Boolean; virtual;
    procedure DeleteRecord; virtual;
    procedure SaveRecord; virtual;
    property KeyFieldName: string read FKeyFieldName write FKeyFieldName;
    property DataSet: TClientRowSet read FDataSet write FDataSet default nil;
    property DataSource: TDataSource read FDataSource write FDataSource default nil;
    property CustomDeleteEvent: Boolean read FCustomDeleteEvent write FCustomDeleteEvent default False;
    property CustomSaveEvent: Boolean read FCustomSaveEvent write FCustomSaveEvent default False;
    procedure StateChangeEvent; virtual;
  public
    { Public declarations }
  end;

var
  FrmParentBasic: TFrmParentBasic;

implementation
uses uCommFunction;
{$R *.dfm}

procedure TFrmParentBasic.actNewExecute(Sender: TObject);
var
  ATag: Integer;
begin
  if FDataSet = nil then
    Exit;
  ATag := (Sender as TAction).Tag;
  case ATag of
    0 : FDataSet.Append;
    1 : DataSetAction(FKeyFieldName, FDataSet, adEDIT);
    2 :
      begin
        if not BeforePostCheck then
          Exit;
        if FCustomSaveEvent then
          SaveRecord
        else
        begin
          try
            FDataSet.Post;
            FDataSet.ApplyUpdates;
          except
            on e:SqlVersionError do
            begin
              DialogInformation(E.Message);
              FDataSet.CancelUpdates;
            end;

            on e:exception do
            begin
              DialogInformation(E.Message);
              FDataSet.CancelUpdates;
            end;
          end;
        end;
      end;
    3 :
      begin
        FDataSet.Cancel;
        FDataSet.CancelUpdates;
      end;
    4 :
      begin
        if FCustomDeleteEvent then
          DeleteRecord
        else
        begin
          try
            DataSetAction(FKeyFieldName, FDataSet, adDELETE);
          except
            on e:SqlVersionError do
            begin
              DialogInformation(E.Message);
              FDataSet.CancelUpdates;
            end;

            on e:exception do
            begin
              DialogInformation(E.Message);
              FDataSet.CancelUpdates;
            end;
          end;
        end;
      end;
    5 : DataSetRefresh(FDataSet, FKeyFieldName);
  end;
end;

function TFrmParentBasic.BeforePostCheck: Boolean;
begin

end;

procedure TFrmParentBasic.DataChangeExt(Sender: TObject; Field: TField);
begin
  if FDataSet = nil then
    Exit;
  if FDataSet.State = dsBrowse then
  begin
    actEdit.Enabled := FDataSet.RecordCount > 0;
    actDelete.Enabled := actEdit.Enabled;
  end;
end;

procedure TFrmParentBasic.DeleteRecord;
begin

end;

procedure TFrmParentBasic.FormCreate(Sender: TObject);
begin
  if FDataSource = nil then
    FDataSource := tvBasic.DataController.DataSource;
  if FDataSource <> nil then
  begin
    FDataSource.OnStateChange := StateChangeExt;
    FDataSource.OnDataChange := DataChangeExt;
  end;

end;

procedure TFrmParentBasic.SaveRecord;
begin

end;

procedure TFrmParentBasic.StateChangeEvent;
begin

end;

procedure TFrmParentBasic.StateChangeExt(Sender: TObject);
begin
  if FDataSet = nil then
    Exit;
  case FDataSet.State of
    dsBrowse :
      begin
        actNew.Enabled := True;
        actRefresh.Enabled := True;
        actCancel.Enabled := False;
        actSave.Enabled := False;
        gbSN.Enabled := False;
      end;
    dsInsert,
    dsEdit :
      begin
        gbSN.Enabled := True;
        actNew.Enabled := False;
        actEdit.Enabled := False;
        actDelete.Enabled := False;
        actRefresh.Enabled := False;
        actSave.Enabled := True;
        actCancel.Enabled := True;
      end;
  end;
  StateChangeEvent;
end;

end.
